class Game {
  constructor() {
    this.canvas = document.getElementById("canvas");
    //创建一个绘制环境 2d环境
    this.draw = this
      .canvas
      .getContext("2d");
    let W = document.documentElement.clientWidth > 420
      ? 420
      : document.documentElement.clientWidth;
    let H = document.documentElement.clientHeight > 750
      ? 750
      : document.documentElement.clientHeight;
    this.canvas.width = W;
    this.canvas.height = H;
    this.timer = null;
    this.frame = 0;
    this.score = 0;
    this.scene = 0;
    if(!localStorage.getItem("FB")){
      localStorage.setItem("FB","[]");
    }
    this.imgLoad();
  };
  clear() {
    this.draw.clearRect(0,0,this.canvas.width,this.canvas.height);
  };
  start() {
    this.SM=new SceneManager();
    this.SM.enter(this.scene);
    this.timer=setInterval(()=>{
      this.frame++;
      this.SM.updateAndRender();      
    },20)
  };
  imgLoad(){
    this.allImg={
    "bg_day":"images/bg_day.png",
    "bg_night":"images/bg_night.png",
    "land":"images/land.png",
    "pipe_down":"images/pipe_down.png",
    "pipe_up":"images/pipe_up.png",
    "bird0_0":"images/bird0_0.png",
    "bird0_1":"images/bird0_1.png",
    "bird0_2":"images/bird0_2.png",
    "title":"images/title.png",
    "button_play":"images/button_play.png",
    "tutorial":"images/tutorial.png",
    "number_score_00":"images/number_score_00.png",
    "number_score_01":"images/number_score_01.png",
    "number_score_02":"images/number_score_02.png",
    "number_score_03":"images/number_score_03.png",
    "number_score_04":"images/number_score_04.png",
    "number_score_05":"images/number_score_05.png",
    "number_score_06":"images/number_score_06.png",
    "number_score_07":"images/number_score_07.png",
    "number_score_08":"images/number_score_08.png",
    "number_score_09":"images/number_score_09.png",
    "blink_00":"images/blink_00.png",
    "blink_01":"images/blink_01.png",
    "blink_02":"images/blink_02.png",
    "medals_0":"images/medals_0.png",
    "medals_1":"images/medals_1.png",
    "medals_2":"images/medals_2.png",
    "medals_3":"images/medals_3.png",
    "game_over":"images/text_game_over.png",
    "score_panel":"images/score_panel.png"
  };
    let count=0,total=Object.keys(this.allImg).length;
    for (let key in this.allImg) {
      let img = new Image();
      img.src=this.allImg[key];
      img.onload=()=>{
        count++;
        this.allImg[key]=img;
        if(count>=total) this.start();
      }
    }
  }
}